package socket.test;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import aos.entity.Message;

public class ClientHandler implements Runnable {
	private Socket socket;

	public ClientHandler(Socket socket) {
		this.socket = socket;
		System.out.println(socket.getInetAddress().getHostAddress() + " "
				+ socket.getPort());
	}

	@Override
	public void run() {
		try {
			ObjectOutputStream os = new ObjectOutputStream(
					socket.getOutputStream());
			ObjectInputStream is = new ObjectInputStream(
					socket.getInputStream());

			Message msg = null;
			while ((msg = (Message) is.readObject()) != null) {
				if (msg.getLogicTime() == 100) {
					break;
				}
				System.out.println(msg.toString());
				msg.setLogicTime(-1);
				os.writeObject(msg);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
	}
}